package com.xc.controller;

import cn.hutool.core.lang.Assert;
import com.xc.dto.ResultDTO;
import com.xc.service.UserService;
import com.xc.entity.UserEntity;
import com.xc.util.JSONCopy;
import com.xc.util.Tool;
import com.xc.vo.UserUpdateVO;
import com.xc.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//restful 增加post，修改put，删除delete，查询get
@RestController
@RequestMapping("/xc/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserEntity userEntity;

    @PostMapping()
    @ApiOperation(value ="用户增加",notes = "用户新增，同时返回当前新增的数据,主要用于新增用户时，需要对角色进行绑定，对于前端来说除了调用，当前API，还需要调用用户角色新增的API接口")
    public ResultDTO add(@RequestBody UserUpdateVO userUpdateVO) {

        return userService.add(JSONCopy.copy(userUpdateVO, UserEntity.class));
    }

    @PutMapping()
    @ApiOperation("用户修改")
    public ResultDTO update(@RequestBody
                                    UserVO userVO) {
        userEntity = JSONCopy.copy(userVO, UserEntity.class);
        return userService.update(userEntity);
    }

    @ApiOperation("用户修改状态enable，1表示正常，0表示未达到标准")
    @PutMapping("/enable")
    public ResultDTO updateEnable(UserVO userVO) {
        userEntity = JSONCopy.copy(userVO, UserEntity.class);
        return userService.updateEnable(userEntity);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("用户删除")
    public ResultDTO delete(UserVO userVO) {
        userEntity.setId(userVO.getId());
        return userService.delete(userEntity);
    }

    //    @GetMapping()
//    @ApiOperation("用户查询,直接第二个参数传递json对象即可")
//    public ResultDTO findAll(@ApiParam(value = "用户名") @RequestParam String username,
//                             @ApiParam(value = "性别") @RequestParam String sex,
//                             @ApiParam(value = "年龄") @RequestParam Integer age,
//                             @ApiParam(value = "手机") @RequestParam String phone,
//                             @ApiParam(value = "生日") @RequestParam String birthday,
//                             @ApiParam(value = "邮箱") @RequestParam String email,
//                             @ApiParam(value = "第几页") @RequestParam String page,
//                             @ApiParam(value = "每页显示条数") @RequestParam String limit) {
////        userEntity = JSONCopy.copy(userVO, UserEntity.class);
//        return userService.findAll(UserEntity.builder()
//                .username(username)
//                .sex(sex)
//                .age(age)
//                .phone(phone)
//                .birthday(birthday)
//                .email(email)
//                .page(Tool.strToPage(page))
//                .limit(Tool.strToPage(limit))
//                .build());
//    }
    @GetMapping()
    @ApiOperation("用户查询")
    public ResultDTO findAll(UserVO userVO) {
//        Assert.notEmpty(userVO.getUsername(),"当前用户名不能为空");
        userEntity = JSONCopy.copy(userVO, UserEntity.class);
        userEntity.setPage(Tool.strToPage(userEntity.getPage() + ""));
        userEntity.setLimit(Tool.strToLimit(userEntity.getLimit() + ""));
        return userService.findAll(userEntity);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户")
    public ResultDTO findById(@ApiParam(value = "Id", required = true) @RequestParam Integer id) {
        userEntity.setId(id);
        return userService.findById(userEntity);
    }

    @GetMapping("/count")
    @ApiOperation("查询总条数")
    public ResultDTO count(UserVO userVO) {
        return userService.count(userEntity);
    }
}
